package com.itcast.demo_one

import android.annotation.SuppressLint
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.Spinner
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class ArrayAdapterActivity : AppCompatActivity() {

    lateinit var citySpinner: Spinner

    // 定义城市列表
    val cityList = listOf<String>("北京", "上海", "广州", "深圳", "杭州", "苏州")


    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_array_adapter)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        // 1. 获取Spinner对象
        citySpinner = findViewById<Spinner>(R.id.city_spinner)
        // 如果设置的模式为dialog
        citySpinner.prompt = "请选择城市"
        // 2. 创建适配器对象
        val adapter = ArrayAdapter(this, R.layout.city_item, cityList)
        // 3. 设置适配器
        citySpinner.adapter = adapter
        // 4. 设置第一个选中
        citySpinner.setSelection(0)
    }
}